﻿using AutoMapper;
using HealthcareSystem.API.Write.Application.Command;
using HealthcareSystem.Domain;
using MediatR;
using WeekExam.PassPort.ErrorCode;
using WeekExam.PassPort.Infrastructure.Interface;

namespace HealthcareSystem.API.Write.Application.CommandHandel
{
    /// <summary>
    /// 注册处理方法
    /// </summary>
    public class RegisterCommandHandel : IRequestHandler<RegisterCommand, ApiResult<int>>
    {
        /// <summary>
        /// 登录注册仓储
        /// </summary>
        IBaseRepository<Login> loginRep;
        /// <summary>
        /// AUtomapping
        /// </summary>
        IMapper mapper;
        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="loginRep">登录注册仓储</param>
        /// <param name="mapper">AUtomapping</param>
        public RegisterCommandHandel(IBaseRepository<Login> loginRep, IMapper mapper)
        {
            this.loginRep = loginRep;
            this.mapper = mapper;
        }
        /// <summary>
        /// 处理
        /// </summary>
        /// <param name="request">请求</param>
        /// <param name="cancellationToken">方法</param>
        /// <returns>处理</returns>
        public async Task<ApiResult<int>> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            var meo = mapper.Map<Login>(request);
            await loginRep.AddAsync(meo);
            return new ApiResult<int>
            {
                Code = ResultCode.Success,
                Data = 1,
                Message = "注册成功"
            };
        }
    }
}
